<?php

namespace Modules\Agent\Entities;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;
use Setting;
use DB;
use Asset;

class InviteRecord2 extends Model
{
    protected $guarded = [];
    protected $table = 'invite_records2';
    static public $resourceName = '';

    protected $casts = [
        'base_reward' => 'json',
        'added_reward' => 'json',
        'node_reward' => 'json'
    ];

    public function user()
    {
        return $this->belongsTo('\Modules\User\Entities\User', 'user_id');
    }

    public function direct_inviter()
    {
        return $this->belongsTo('\Modules\User\Entities\User', 'direct_inviter_id');
    }

    public function indirect_inviter()
    {
        return $this->belongsTo('\Modules\User\Entities\User', 'indirect_inviter_id');
    }

    public function scopeSearch($query, $key)
    {
        return $query->whereHas('user', function ($query) use ($key) {
            return $query->where('name', 'like',  '%' . $key . '%')->orWhere('phone', $key);
        })->orWhereHas('direct_inviter', function ($query) use ($key) {
            return $query->where('name', 'like',  '%' . $key . '%')->orWhere('phone', $key);
        })->orWhereHas('indirect_inviter', function ($query) use ($key) {
            return $query->where('name', 'like',  '%' . $key . '%')->orWhere('phone', $key);
        });
    }

    // 更新某节点内的分享序号
    public function updateNodeInviteIndex()
    {
        $total = InviteRecord2::where([
            'direct_inviter_id' => $this->direct_inviter_id,
            'invite_node_id' => $this->invite_node_id,
            ['id', '<', $this->id]
        ])->count();

        $this->update([
            'node_invite_index' => $total + 1
        ]);
    }

    static public function createWith($order)
    {
        $orderTypes = ['product', 'activity'];

        // 不在目标内的订单和没有邀请人的订单，不需要发放佣金
        // 没有支付
        $noPayFlag = !$order->pay_money_price && !$order->pay_score_price &&  !$order->redpack_discount && !$order->score_used;

        // 订单类型无佣金 或 订单无支付
        if (!in_array($order->type, $orderTypes) || $noPayFlag) {
            // 无佣金
            $order->update([
                'share_status' => 1
            ]);
            return null;
        }

        $brokerageList = [];
        $item = static::createWithOrderSku($order);
        if ($item) {
            array_push($brokerageList, $item);
        }

        // 没有佣金记录
        if (count($brokerageList) === 0) {
            $order->update([
                'share_status' => 1
            ]);
            return null;
        }
        else {
            $order->update([
                'share_status' => 2
            ]);
        }
    }

    static public function createWithOrderSku($order)
    {
        // 自己点击自己的分享链接
        if ( $order->sharer == $order->user_id )
            return false;

        // 判断此人是否已兑换过此奖
        $total = InviteRecord2::where([
            'user_id' => $order->user_id,
            'invite_node_id' => $order->node_id,
            'invite_node_type' => $order->node_type,
            'invite_node_uuid' => $order->node_uuid,
        ])->count();

        if ( $total > 0 ){
            return false;
        }

        $inviteIndex = InviteRecord2::where([
            'direct_inviter_id' => $order->sharer,
            'invite_node_id' => $order->node_id,
            'invite_node_type' => $order->node_type,
            'invite_node_uuid' => $order->node_uuid,
        ])->count();

        $info = [
            'status' => 9,
            'direct_inviter_id' => $order->sharer,
            'user_id' => $order->user_id,
            'order_id' => $order->id,
            'order_uuid' => $order->uuid,
            'invite_index' => $inviteIndex+1,
            'invite_at' => date('Y-m-d H:i:s'),
            'invite_node_type' => $order->node_type,
            'invite_node_id' => $order->node_id,
            'invite_node_uuid' => $order->node_uuid,
        ];

        $item = self::create($info);

        return $item;
    }



}
